package com.huawei.hms.framework.network.restclient;

import com.huawei.hms.framework.network.restclient.Response.Builder;
import com.huawei.hms.framework.network.restclient.hwhttp.Request;
import com.huawei.hms.framework.network.restclient.hwhttp.Response;
import com.huawei.hms.framework.network.restclient.hwhttp.Submit;
import com.huawei.hms.framework.network.restclient.hwhttp.Submit.Factory;
import java.io.IOException;
import javax.annotation.concurrent.GuardedBy;

public class RealSubmit<T> implements Submit {
    private static final String TAG = "RealSubmit";
    private final Object[] args;
    private volatile boolean canceled;
    @GuardedBy("this")
    private boolean executed;
    Factory factory;
    Submit submit;
    private final SubmitMethod<T, ?> submitMethod;

    public RealSubmit(Factory factory, SubmitMethod<T, ?> submitMethod, Object[] objArr) {
        this.factory = factory;
        this.submitMethod = submitMethod;
        this.args = objArr;
    }

    public synchronized Request request() throws IOException {
        if (this.submit == null) {
            this.submit = createSubmit();
        }
        return this.submit.request();
    }

    public Response execute() throws IOException {
        synchronized (this) {
            if (this.executed) {
                throw new IllegalStateException("Already Executed");
            }
            this.executed = true;
        }
        if (this.submit == null) {
            this.submit = createSubmit();
        }
        return ParcelResponse(this.submit.execute());
    }

    public void enqueue(ResultCallback resultCallback) {
        synchronized (this) {
            if (this.executed) {
                throw new IllegalStateException("Already Executed");
            }
            this.executed = true;
        }
        try {
            if (this.submit == null) {
                this.submit = createSubmit();
            }
            this.submit.enqueue(new 1(this, resultCallback));
        } catch (Throwable e) {
            resultCallback.onFailure(e);
        }
    }

    private Submit createSubmit() throws IOException {
        Submit newSubmit = this.factory.newSubmit(this.submitMethod.toRequest(this.args));
        if (newSubmit != null) {
            return newSubmit;
        }
        throw new IOException("create submit error");
    }

    public void cancel() {
        this.canceled = true;
        if (this.submit != null) {
            this.submit.cancel();
        }
    }

    public synchronized boolean isExecuted() {
        return this.executed;
    }

    public boolean isCanceled() {
        return this.canceled || (this.submit != null && this.submit.isCanceled());
    }

    public Submit clone() {
        return new RealSubmit(this.factory, this.submitMethod, this.args);
    }

    private Response ParcelResponse(Response response) throws IOException {
        Builder builder = new Builder();
        builder.code(response.getCode()).message(response.getMessage()).headers(response.getHeaders());
        if (!response.isSuccessful()) {
            builder.errorBody(response.getBody().bytes());
            response.getBody().close();
        } else if (response.getCode() == 204 || response.getCode() == 205) {
            builder.body(null);
        } else if (this.submitMethod == null) {
            builder.body(null);
        } else {
            builder.body(this.submitMethod.toResponse(response.getBody()));
        }
        return builder.build();
    }
}
